package Concurrency2.Chapter01_Base;

import java.util.concurrent.TimeUnit;

/**
 * Instruction：
 * Author：@author MaLi
 */
public class T06_ThreadFunction {
    public static void main(String[] args) {
//        testSleep();
//        testInterrupted();
//        testDaemon();
    }

    private static void testDaemon() {
        Thread t = new Thread(() -> {
            Thread cu = Thread.currentThread();
            System.out.println(cu.getName() + "-->" + cu.isDaemon());
            try {
                TimeUnit.SECONDS.sleep(2);
            } catch (InterruptedException e) {
                throw new RuntimeException(e);
            }
            System.out.println("lambda stop");
        });
        t.setDaemon(true);//如果设定了守护线程,
        t.start();
        System.out.println("Main thread");
    }

    private static void testInterrupted() {
        Thread thread_toInterrupted = new Thread(() -> {
            try {
                System.out.println("----------------------------");
                Thread.sleep(100000);
            } catch (InterruptedException e) {
                System.out.println("---> interrupted");
                throw new RuntimeException(e);
            }
        }, "thread_toInterrupted");
        thread_toInterrupted.start();
        thread_toInterrupted.interrupt();
    }

    private static void testSleep() throws InterruptedException {
        Thread t_sleep = new Thread(() -> {
            try {
                System.out.println("----------------------------");
                Thread.sleep(100000);
            } catch (InterruptedException e) {
                throw new RuntimeException(e);
            }
        }, "Thread_sleepstate");

        t_sleep.start();
        t_sleep.join();
    }
}
